package tech.mobi.child.utils;

import java.lang.reflect.Field;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.util.Log;

public class SqliteParser {

	public <T> List<T> parseDatabases(Cursor cursor,Class<T> clazz) {
		List<T> objList = new ArrayList<T>();
		Field[] fields = clazz.getDeclaredFields();
		Field field = null;
		String columnValue = null;
		int columnCount = cursor.getColumnCount();
		String[] names = cursor.getColumnNames();
		String columnName = null;
		int len = fields.length;
		while(cursor.moveToNext()){
			T obj = null;
			try {
				obj = clazz.newInstance();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InstantiationException e) {
				e.printStackTrace();
			}
			if(obj!=null){
				for(int i=0;i<columnCount;i++){
					columnName = names[i];
					for(int j=0;j<len;j++){
						field = fields[j];
						if(field.getName().equalsIgnoreCase(columnName)){
							Class<?> cClass = field.getType();
							if(cClass.getName().equals("double") 
									|| cClass.getName().equals("java.lang.Double")) {
								columnValue = Tools.GetThousandsString(cursor.getDouble(cursor.getColumnIndex(names[i])));
							}
							else {
								columnValue = cursor.getString(cursor.getColumnIndex(names[i]));
							}
//							if(columnName.equals("JZJE")){
//								Log.i("DB", names[i]+"--"+cursor.getDouble(cursor.getColumnIndex(names[i])));;
//							}
							Log.i("DB", names[i]+"--"+columnValue);
							Object objValue = valueOf(cClass,columnValue);
							Log.i("DB", names[i]+"---"+objValue);
							field.setAccessible(true);
							try {
								field.set(obj, objValue);
							} catch (IllegalArgumentException e) {
								e.printStackTrace();
							} catch (IllegalAccessException e) {
								e.printStackTrace();
							}
						}
					}
				}
			}
			objList.add(obj);
		}
		return objList;
	}

	/**
	 * 把字符串赋值给执行类
	 * 
	 * @param cls
	 * @param value
	 * @return object
	 */
	public Object valueOf(Class<?> cls, String value) {
		Object obj = Types.OTHER;
		try {
			// 如遇基本数据类型, 会抛异常, 比如Integer可以实例化, 而int和long都不能
			obj = cls.newInstance();
			if (obj instanceof Boolean) {
				obj = Boolean.valueOf(value);
			} else if (obj instanceof Byte) {
				obj = Byte.valueOf(value);
			} else if (obj instanceof Date) {
				obj = Date.valueOf(value);
			} else if (obj instanceof Double) {
				obj = Double.valueOf(value);
			} else if (obj instanceof Float) {
				obj = Float.valueOf(value);
			} else if (obj instanceof Integer) {
				obj = Integer.valueOf(value);
			} else if (obj instanceof Long) {
				obj = Long.valueOf(value);
			} else if (obj instanceof Short) {
				obj = Short.valueOf(value);
			} else if (obj instanceof String) {
				obj = String.valueOf(value);
			} else if (obj instanceof Time) {
				obj = Time.valueOf(value);
			} else if (obj instanceof Timestamp) {
				obj = Timestamp.valueOf(value);
			}
		} catch (InstantiationException e) {
			// 基本数据类型的赋值
			String sType = cls.getName();
			try {
				if (sType.equals("byte") ) {
					obj = Byte.parseByte(value);
				} else if (sType.equals("int") || sType.equals("java.lang.Integer")) {
					obj = Integer.parseInt(value);
				} else if (sType.equals("long") || sType.equals("java.lang.Integer")) {
					obj = Long.parseLong(value);
				} else if (sType.equals("float")) {
					obj = Float.parseFloat(value);
				} else if (sType.equals("double") || sType.equals("java.lang.Double")) {
					obj = Double.parseDouble(value);
				} else if (sType.equals("java.math.BigDecimal")) {
					if (value.indexOf('.') < 0) {
						obj = Long.parseLong(value);
					} else {
						obj = Double.parseDouble(value);
					}
				}
			} catch (NumberFormatException ne) {
				obj = Types.OTHER;
			}
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}

		return obj;
	}
}
